package com.ycb.bookserver.service.impl;

import com.book.constant.CategoryStatus;
import com.ycb.book.dto.CategoryDTO;
import com.ycb.book.entity.Category;
import com.ycb.bookserver.mapper.CategoryMapper;
import com.ycb.bookserver.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class CategoryServiceImp implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public Category getById(Long id) {

        return categoryMapper.getById(id);
    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(CategoryStatus.ENABLE);
        categoryMapper.insert(category);
    }

    /**
     * 修改目录信息
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.update(category);
    }

    @Override
    public List<Category> list() {
        return categoryMapper.list();
    }

    /**
     * 用户端获取分类
     * @return
     */
    @Override
    public List<Category> getCategory() {
        List<Category> categoryList=categoryMapper.list();
        List<Category> categoryList1=new ArrayList<>();
        for (Category category : categoryList) {
            if(Objects.equals(category.getStatus(), CategoryStatus.ENABLE)){
                categoryList1.add(category);
            }
        }
        return categoryList1;
    }
}
